package hibernateapplication;

import java.util.List;
import model.Cars;
import model.Klient;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import utils.HibernateUtil;

/**
 * @author Jacek Laskowski
 */
public class Main {

    public static void main(String[] args) {
        Main main = new Main();
        //   if (args.length > 0 && "zapisz".equals(args[0])) {
        //       main.stworzKlienta(args[1], args[2]);
        //   } else {

        main.createCars("Audi", "90", "2.0", "113", "niebieski", "standard");

        List<Cars> cars = main.readCars();

        for (Cars c : cars) {
            System.out.println(c.getId()+" : "+c.getMarka()+" "+c.getModel()+" "+" "+c.getPojemnosc()+" "+c.getMoc()+" km");
        }

        
        HibernateUtil.getSessionFactory().close();
    }

    private void stworzKlienta(String imie, String nazwisko) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        Klient klient = new Klient(imie, nazwisko);
        session.save(klient);

        session.getTransaction().commit();
    }

    private List<Klient> wypiszKlientow() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List result = session.createQuery("from Klient").list();
        session.getTransaction().commit();
        return result;
    }

    private void createCars(String marka, String model, String pojemnosc, String moc, String kolor, String wyposazenie) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            session.beginTransaction();

            Cars cars = new Cars(marka, model, pojemnosc, moc, kolor, wyposazenie);
            session.save(cars);
            session.getTransaction().commit();

        } catch (HibernateException e) {
            e.printStackTrace();
        } finally {
          
        }
    }

    private List<Cars> readCars() {
        List result = null;
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        try {
            session.beginTransaction();
            result = session.createQuery("from Cars").list();
            session.getTransaction().commit();

        } catch (HibernateException e) {
            e.printStackTrace();
        } finally {
            
        }
        return result;
    }

    public void deleteCars_object(long id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
         try {
        session.beginTransaction();     
        Cars cars = new Cars();  
        cars.setId(id);
        session.delete(cars);  
        session.getTransaction().commit();  
       } catch (HibernateException e) {
            e.printStackTrace();
        } finally {
            
        }
    }

    public void updateEmployee(long id, String marka) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            String queryString = "from Employee where id = :id";
            Query query = session.createQuery(queryString);
            query.setLong("id", id);
            Cars cars = (Cars) query.uniqueResult();
            cars.setMarka(marka);
            session.update(cars);
            System.out.println("One employee is updated!");
        } catch (HibernateException e) {
            e.printStackTrace();
        } finally {
           
        }
    }
}